﻿#include "FITKAdaptorGeoModelSphere.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelSphere.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKHDF5FileTool.h"


namespace Interface
{
    bool  FITKAdaptorGeoModelSphere::readParameters()
    {
        Interface::FITKAbsGeoModelSphere* cmd = this->getDataObjectAs<Interface::FITKAbsGeoModelSphere>();
        if (cmd == nullptr || _reader == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        std::tuple<bool, H5::Group> paramGroup = h5tools->openGp(*_h5Group, FGKW_Parameter);
        if (!std::get<0>(paramGroup)) return false;
        double radius = 0;
        double location[3] = { 0.0, 0.0, 0.0 };
        if(!h5tools->readGroupAttrDoubleArray(std::get<1>(paramGroup), FGKW_Location, location, 1, 3))return false;
        if(!h5tools->readGroupDouble(std::get<1>(paramGroup), FGKW_Radius, &radius))return false;
        cmd->setRadius(radius);
        cmd->setLocation(location);
        return true;
    }
    bool FITKAdaptorGeoModelSphere::writeParameters(H5::Group &g)
    {
        Interface::FITKAbsGeoModelSphere* cmd = this->getDataObjectAs<Interface::FITKAbsGeoModelSphere>();
        if (cmd == nullptr || _writer == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _writer->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        //创建group组
        std::tuple<bool,H5::Group> paramGroup = h5tools->createGp(g, FGKW_Parameter);
        if(!std::get<0>(paramGroup)) return false;
        
        double radius = cmd->getRadius();
        double location[3] = {0.0, 0.0, 0.0};
        cmd->getLocation(location);

        //写出参数
        h5tools->writeGroupAttrDoubleArray(std::get<1>(paramGroup), FGKW_Location, location,1,3);
        h5tools->writeGroupAttrDouble(std::get<1>(paramGroup), FGKW_Radius, radius);

        return true;
    }

}



